{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to the Register Map\n",
    "\n",
    "It is possible to access the register map for IP in the overlay directly. This functionality requires that the `.hwh` file is distributed along with the bitstream. This can be generated from Vivado.\n",
    "\n",
    "In this example we are going us the GPIO blocks controlling the buttons and LEDs to demonstrate how to explore and interact with the register map.\n",
    "\n",
    "First we need to import the base overlay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "\n",
    "base = BaseOverlay('base.bit')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we get the drivers for "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "btns = base.gpio_btns\n",
    "leds = base.gpio_leds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now print the register map and the current values of the registers by printing the representation of the `.register_map`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RegisterMap {\n",
       "  GPIO_DATA = Register(Channel_1_GPIO_DATA=0),\n",
       "  GPIO_TRI = Register(Channel_1_GPIO_TRI=15),\n",
       "  GPIO2_DATA = Register(Channel_2_GPIO_DATA=0),\n",
       "  GPIO2_TRI = Register(Channel_2_GPIO_TRI=4294967295),\n",
       "  GIER = Register(Global_Interrupt_Enable=1),\n",
       "  IP_IER = Register(Channel_1_Interrupt_Enable=0, Channel_2_Interrupt_Enable=0),\n",
       "  IP_ISR = Register(Channel_1_Interrupt_Status=0, Channel_2_Interrupt_Status=0)\n",
       "}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "btns.register_map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To access values programmatically we can walk the object model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "btns.register_map.GPIO_DATA.Channel_1_GPIO_DATA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If there any more information on a register is available it can be accessed using `help`. This will list any description of the register available as well as the subfields. Here we see that the `IP_IER` register the interrupt enable register and contains two fields - one for each channel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on RegisterIP_IER in module pynq.register_map object:\n",
      "\n",
      "class RegisterIP_IER(Register)\n",
      " |  IP Interrupt Enable register\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      RegisterIP_IER\n",
      " |      Register\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  Channel_1_Interrupt_Enable\n",
      " |      IP Interrupt Enable register\n",
      " |  \n",
      " |  Channel_2_Interrupt_Enable\n",
      " |      IP Interrupt Enable register\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  _fields = {'Channel_1_Interrupt_Enable': {'access': 'read-write', 'bit...\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from Register:\n",
      " |  \n",
      " |  __getitem__(self, index)\n",
      " |      Get the register value.\n",
      " |      \n",
      " |      This method accepts both integer index, or slice as input parameters.\n",
      " |      \n",
      " |      Parameters\n",
      " |      ----------\n",
      " |      index : int | slice\n",
      " |          The integer index, or slice to access the register value.\n",
      " |  \n",
      " |  __index__(self)\n",
      " |      Return an index containing the value of the register\n",
      " |  \n",
      " |  __init__(self, address, width=32, debug=False, buffer=None)\n",
      " |      Instantiate a register object.\n",
      " |      \n",
      " |      Parameters\n",
      " |      ----------\n",
      " |      address : int\n",
      " |          The address of the register.\n",
      " |      width : int\n",
      " |          The width of the register, e.g., 32 (default) or 64.\n",
      " |      debug : bool\n",
      " |          Turn on debug mode if True; default is False.\n",
      " |      buffer : Buffer\n",
      " |          Buffer object to use for reading and writing the value\n",
      " |          of the register. If None the address is assumed to be\n",
      " |          an absolute physical address\n",
      " |  \n",
      " |  __int__(self)\n",
      " |      Return an integer of the value of the register\n",
      " |  \n",
      " |  __repr__(self)\n",
      " |      Print a representation of the Register and all its fields\n",
      " |      \n",
      " |      If the Register has been subclassed with fields then these\n",
      " |      will be printed otherwise the return string will contain the\n",
      " |      value of the entire register\n",
      " |  \n",
      " |  __setitem__(self, index, value)\n",
      " |      Set the register value.\n",
      " |      \n",
      " |      This method accepts both integer index, or slice as input parameters.\n",
      " |      \n",
      " |      Parameters\n",
      " |      ----------\n",
      " |      index : int | slice\n",
      " |          The integer index, or slice to access the register value.\n",
      " |  \n",
      " |  __str__(self)\n",
      " |      Print the register value.\n",
      " |      \n",
      " |      This method is overloaded to print the register value. The output\n",
      " |      is a string in hex format.\n",
      " |  \n",
      " |  _debug(self, s, *args)\n",
      " |      The method provides debug capabilities for this class.\n",
      " |      \n",
      " |      Parameters\n",
      " |      ----------\n",
      " |      s : str\n",
      " |          The debug information format string\n",
      " |      *args : any\n",
      " |          The arguments to be formatted\n",
      " |      Returns\n",
      " |      -------\n",
      " |      None\n",
      " |  \n",
      " |  _reordered_setitem(self, value, index)\n",
      " |      Wrapped version of __setitem__ for better use with\n",
      " |      functools.partial\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from Register:\n",
      " |  \n",
      " |  count(index, width=32) from builtins.type\n",
      " |      Provide the number of bits accessed by an index or slice\n",
      " |      \n",
      " |      This method accepts both integer index, or slice as input parameters.\n",
      " |      \n",
      " |      Parameters\n",
      " |      ----------\n",
      " |      index : int | slice\n",
      " |          The integer index, or slice to access the register value.\n",
      " |      width : int\n",
      " |          The number of bits accessed.\n",
      " |  \n",
      " |  create_subclass(name, fields, doc=None) from builtins.type\n",
      " |      Create a subclass of Register that has properties for the\n",
      " |      specified fields\n",
      " |      \n",
      " |      Parameters\n",
      " |      ----------\n",
      " |      name : str\n",
      " |          A suffix for the name of the subclass\n",
      " |      fields : dict\n",
      " |          A Dictionary containing the fields to add to the subclass\n",
      " |      \n",
      " |      The fields should be in the form used by the ip_dict, namely\n",
      " |      name -> {\n",
      " |          access = \"read-only\" | \"read-write\" | \"write-only\",\n",
      " |          bit_offset: int, bit_width: int, description: str\n",
      " |      }\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors inherited from Register:\n",
      " |  \n",
      " |  __dict__\n",
      " |      dictionary for instance variables (if defined)\n",
      " |  \n",
      " |  __weakref__\n",
      " |      list of weak references to the object (if defined)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(btns.register_map.IP_IER)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Writing can be performed either to registers or fields. Note that register slice access are performed using the RTL conventions of hi:lo inclusive to match the format commonly seen in data sheets rather than the lo:hi inclusive-exclusive more common in Python ranges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "leds.register_map.GPIO_DATA.Channel_1_GPIO_DATA = 8\n",
    "leds.register_map.GPIO_DATA[2:0] = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At present this functionality is limited to scalar registers in IP that has the attached metadata. This includes most IP in the Xilinx IP catelog and any HLS-generated IP that uses AXI-lite for control registers."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
